package com.nondo.dean.order.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.nondo.dean.order.domain.OrderDetail;
import com.nondo.dean.order.domain.OrderDetailExample;
import com.nondo.dean.order.domain.OrderSubject;
import com.nondo.dean.order.mapper.OrderDetailMapper;

/**
 * 订单明细管理业务类
 * 
 * @author zhaoljin
 * @date 2018年4月19日
 */
@Component
public class OrderDetailService {

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    /**
     * 插入订单明细
     * @param orderDetail
     * @return
     */
    @Transactional
    public int addOrderDetail(OrderDetail orderDetail) {
        return orderDetailMapper.insertSelective(orderDetail);
    }

    /**
     * 修改订单明细
     * @param orderDetail
     * @return
     */
    @Transactional
    public int updateOrderDetail(OrderDetail orderDetail) {
        return orderDetailMapper.updateByPrimaryKeySelective(orderDetail);
    }

    /**
     * 按照主键ID查询订单明细
     * @param id
     * @return
     */
    public OrderDetail selectOrderDetail(Integer id) {
        return orderDetailMapper.selectByPrimaryKey(id);
    }

    /**
     * 订单明细分页查询列表
     * @param offset
     * @param limit
     * @param orderDetail
     * @return
     */
    public List<OrderDetail> getOrderDetailList(int offset, int limit, OrderDetail orderDetail) {
        OrderDetailExample example = new OrderDetailExample();
        example.setOrderByClause("create_date DESC");
        RowBounds rowBounds = new RowBounds(offset, limit);
        OrderDetailExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, orderDetail);
        return orderDetailMapper.selectByExampleWithRowbounds(example, rowBounds);
    }

    /**
     * 订单明细总数统计
     * @param orderDetail
     * @return
     */
    public Long count(OrderDetail orderDetail) {
        OrderDetailExample example = new OrderDetailExample();
        OrderDetailExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, orderDetail);
        return orderDetailMapper.countByExample(example);
    }

    /**
     * 组装查询条件
     * @param criteria
     * @param orderDetail
     */
    void setCriteria(OrderDetailExample.Criteria criteria, OrderDetail orderDetail) {
        if (orderDetail != null) {
            if (orderDetail.getId() != null)
                criteria.andIdEqualTo(orderDetail.getId());
            if (StringUtils.isNotBlank(orderDetail.getOrderCode()))
                criteria.andOrderCodeLike("%" + orderDetail.getOrderCode() + "%");
        }
        criteria.andDelFlagEqualTo(String.valueOf(0));
    }

    /**
     * 按照订单号查询订单明细列表
     * @param orderCode 订单编码
     * @return 订单明细列表(明细只含主件产品)
     */
    public List<OrderDetail> getOrderDetailList(String orderCode) {
        OrderDetailExample example = new OrderDetailExample();
        example.setOrderByClause("create_date DESC");
        OrderDetailExample.Criteria criteria = example.createCriteria();
        criteria.andOrderCodeEqualTo(orderCode);
        criteria.andUpIdEqualTo(0);
        criteria.andDelFlagEqualTo(OrderSubject.DEL_FLAG_NORMAL);
        return orderDetailMapper.selectByExample(example);
    }

    /**
     * 按照订单号查询订单明细列表
     * @param orderId 订单ID
     * @return 订单明细列表
     */
    public List<OrderDetail> getOrderDetailList(Integer orderId) {
        OrderDetailExample example = new OrderDetailExample();
        example.setOrderByClause("create_date DESC");
        OrderDetailExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        criteria.andDelFlagEqualTo(String.valueOf(0));
        return orderDetailMapper.selectByExample(example);
    }

    /**
     * 按照父级订单明细查询
     * @param orderId 父级明细ID
     * @return 订单明细列表
     */
    public List<OrderDetail> getOrderDetailListByUpId(Integer orderId) {
        if (orderId == null) return new ArrayList<OrderDetail>();
        OrderDetailExample example = new OrderDetailExample();
        example.setOrderByClause("create_date DESC");
        OrderDetailExample.Criteria criteria = example.createCriteria();
        criteria.andUpIdEqualTo(orderId);
        criteria.andDelFlagEqualTo(String.valueOf(0));
        return orderDetailMapper.selectByExample(example);
    }
}
